<?php

$product_id = $_GET["id"];
if(!$product_id) header("location: product_order.php");
$product_id = (int)$product_id;

?>
<!DOCTYPE HTML>
<html>
	<head>
		<title>Product Vote</title>
		<?php include '_head_init.php' ?>
	</head>
	<body>
		<?php include '_navbar.php' ?>
		<div class="container">
			<div class="row padding1">
				<span class="pages-list">
					<a href="#" class="btn btn-link page-link"></a>
				</span>
				<span>
					<a href="product_share.php?id=<?php echo $product_id; ?>"
					 class="btn btn-link f-right">
						<span class="badge badge-info link">查看评论</span>
					</a>
					<a href="product_order.php" class="btn btn-link f-right">
						<span class="badge badge-info link">查看排序</span>
					</a>
					<a href="product_list_v2.php" class="btn btn-link f-right">
						<span class="badge badge-info link">返回列表</span>
					</a>
				</span>
			</div>
			<div class="row">
				<div class="span8">
					<table class="table table-bordered table-hover view-table">
						<caption></caption>
						<thead>
							<tr>
								<th width="305">投票人</th>
								<th width="305">发表时间</th>
							</tr>
						</thead>
						<tbody id="vote-list">
							<tr>
								<td></td>
								<td></td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
			<div class="row padding1">
				<span class="pages-list">
				</span>
			</div>
		</div> <!-- end container -->
<script type="text/javascript">

jQuery(function() {
	var product_id = <?php echo $product_id; ?>;
	var tr = $('#vote-list tr:first').clone();
	var vote_step = 50;
	var vote_start_index = 1;
	var vote_sum = 0;
	var page_sum = 1;

	function getVoteList() {
		var serverReturn = _get(
			'/app/index.php?'
			+'d=api&c=vote&m=get_product_vote_list'
			+'&product_id=<?php echo $product_id; ?>'
			+'&start_index='+vote_start_index
			+'&step='+vote_step+'');
		if(vote_sum != serverReturn.sum){
			vote_sum = serverReturn.sum;
			updateVotePage();
		}
		return serverReturn.vote_list;
	}

	function generateVoteTr(vote) {
		var trClone = tr.clone();
		trClone.attr('vote-id', vote['vote_id']);
		var tdList = trClone.find('td');
		$(tdList[0]).html(vote['user_wb_screen_name']);
		$(tdList[1]).html(vote['vote_create_time']);
		return trClone;
	}

	function updateVoteGrid(){
		$('#vote-list').empty();
		var votes = getVoteList();
		for(var i in votes){
			$('#vote-list').append( generateVoteTr( votes[i] ) );
		}
	}

	var a = $('.pages-list:first a:first').clone();

	function generateVoteSpan(page_number, span_page){
		$(span_page).empty();
		if(page_number == 1) return true;
		for(var j = 1; j <= page_number; j++){
			var aClone = a.clone();
			aClone.attr('page-number', j);
			aClone.bind('click', function(){
				var page_number = $(this).attr('page-number');
				var new_start_index = ( page_number - 1 ) * vote_step + 1;
				if(vote_start_index != new_start_index){
					vote_start_index = new_start_index;
					updateVoteGrid();
					showLimitPageLink(page_number);
				}
				return false;
			});
			$(aClone).html(j);
			$(aClone).hide();
			$(span_page.append(aClone));
		}
	}

	function updateVotePage(){
		page_sum = (vote_sum - vote_sum % vote_step) / vote_step + 
			( vote_sum % vote_step ? 1 : 0 );
		generateVoteSpan(page_sum, $('.pages-list:first'));
		if(vote_sum > vote_step){
			generateVoteSpan(page_sum, $('.pages-list:last'));
		}
	}

	function showLimitPageLink(page_number){
		var page_link_radius = 3;
		if(page_sum < 2) return true; // show none
		$(".page-link").hide();
		if(page_sum < 2 * (page_link_radius + 1)){ // few page - show all
			$(".page-link").attr("style", "display:inline;");
			return true;
		}
		if(page_number <= page_link_radius){ // first pages - show first all
			for(var i = 0; i < page_link_radius * 2 + 1; i++){
				$('.page-link[page-number="' + (i + 1) + '"]').attr("style", "display:inline;");
			}
		}else if(page_sum <= page_number + page_link_radius){ // last pages - show last all
			for(var i = 0; i < page_link_radius * 2 + 1; i++){
				$('.page-link[page-number="' + (page_sum - i) + '"]').attr("style", "display:inline;");
			}
		}else{ // show page by current page-number
			for(var i = page_number - page_link_radius; i < page_number + page_link_radius; i++){
				$('.page-link[page-number="' + i + '"]').attr("style", "display:inline;");
			}
		}
	}

	updateVoteGrid();
	showLimitPageLink(1);
});

</script>
	</body>
</html>